package com.blrs.service.file.impl;

import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;

import com.blrs.common.exception.ServiceException;
import com.blrs.common.utils.SecurityUtils;
import com.blrs.domain.MainType;
import com.blrs.mapper.InternalDocumentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.blrs.mapper.FileCabinetInformationMapper;
import com.blrs.domain.FileCabinetInformation;
import com.blrs.service.file.IFileCabinetInformationService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 文件柜维护Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-01
 */
@Service
public class FileCabinetInformationServiceImpl implements IFileCabinetInformationService 
{
    @Autowired
    private FileCabinetInformationMapper fileCabinetInformationMapper;
    @Autowired
    private InternalDocumentMapper internalDocumentMapper;



    @Override
    public List<FileCabinetInformation> selectExternalDocumentPermissionList(FileCabinetInformation fileCabinetInformation)
    {
        Long userid = SecurityUtils.getUserId();
        if(userid!=1){
            fileCabinetInformation.setBelongTo(SecurityUtils.getOrgId());
        }
        return fileCabinetInformationMapper.selectExternalDocumentPermissionList(fileCabinetInformation);
    }
    @Override
    public List<Map> getFileTree(FileCabinetInformation fileCabinetInformation) {
        List<FileCabinetInformation> types=fileCabinetInformationMapper.selectFileCabinetInformationListFileTree(fileCabinetInformation);
        List<FileCabinetInformation> fList=types.stream().filter(obj->obj.getSuperiorCabinetId().equals("00000000-0000-0000-0000-000000000000")).collect(Collectors.toList());
        List<FileCabinetInformation> zList=types.stream().filter(obj->!obj.getSuperiorCabinetId().equals("00000000-0000-0000-0000-000000000000")).collect(Collectors.toList());
        List<Map> list=new ArrayList<>();
        for (FileCabinetInformation info : fList) {
            Map map=new HashMap<>();
            map.put("id",info.getCabinetId());
            map.put("idPath",info.getIdPath());
            map.put("parentId",info.getSuperiorCabinetId());//上级
            map.put("label",info.getCabinetName());
            List<Map> listMap=list(zList,info.getCabinetId());
            if(listMap.size()!=0){
                map.put("children",listMap);
            }
            list.add(map);
        }
        return list;
    }

    private List<Map> list(List<FileCabinetInformation> zList, String parentId){
        List<Map> list=new ArrayList<>();
        for (FileCabinetInformation info : zList) {
            if(info.getSuperiorCabinetId().equals(parentId)){
                Map map=new HashMap<>();
                map.put("id",info.getCabinetId());
                map.put("idPath",info.getIdPath());

                map.put("label",info.getCabinetName());
                List<Map> listMap=list(zList,info.getCabinetId());
                if(listMap.size()!=0){
                    map.put("children",listMap);
                }
                list.add(map);
            }
        }
        return list;
    }

    /**
     * 查询文件柜维护
     * 
     * @param cabinetId 文件柜维护主键
     * @return 文件柜维护
     */
    @Override
    public FileCabinetInformation selectFileCabinetInformationByCabinetId(String cabinetId)
    {
        return fileCabinetInformationMapper.selectFileCabinetInformationByCabinetId(cabinetId);
    }

    @Override
    public FileCabinetInformation selectFileCabinetInformationByCabinetName(String cabinetName,String belongTo)
    {
        return fileCabinetInformationMapper.selectFileCabinetInformationByCabinetName(cabinetName,belongTo);
    }

    /**
     * 查询文件柜维护列表
     * 
     * @param fileCabinetInformation 文件柜维护
     * @return 文件柜维护
     */
    @Override
    public List<FileCabinetInformation> selectFileCabinetInformationList(FileCabinetInformation fileCabinetInformation)
    {
        return fileCabinetInformationMapper.selectFileCabinetInformationList(fileCabinetInformation);
    }

    @Override
    public List<FileCabinetInformation> selectFileCabinetInformationListFileTree(FileCabinetInformation fileCabinetInformation)
    {
        return fileCabinetInformationMapper.selectFileCabinetInformationListFileTree(fileCabinetInformation);
    }

    /**
     * 新增文件柜维护
     * 
     * @param fileCabinetInformation 文件柜维护
     * @return 结果
     */
    @Override
    public int insertFileCabinetInformation(FileCabinetInformation fileCabinetInformation)
    {
        //判断新增的文件夹是否已经存在
        if (fileCabinetInformationMapper.selectIsExit(fileCabinetInformation)>0){
            //说明存在
            return 0;
        }
        fileCabinetInformation.setCabinetId(UUID.randomUUID().toString());
        FileCabinetInformation data = selectFileCabinetInformationByCabinetId(fileCabinetInformation.getSuperiorCabinetId());
       fileCabinetInformation.setSuperiorCabinetName(data.getCabinetName());//上级文件柜
        fileCabinetInformation.setCreationId(SecurityUtils.getUserId());
        fileCabinetInformation.setCreationName(SecurityUtils.getUsername());
        fileCabinetInformation.setCreationTime(new Timestamp(new Date().getTime()));
        fileCabinetInformation.setOrganizationId(SecurityUtils.getOrgId());
        fileCabinetInformation.setOrganizationName(SecurityUtils.getOrgName());
        fileCabinetInformation.setDepartmentId(SecurityUtils.getDeptId());
        fileCabinetInformation.setOperateTime(new Timestamp(new Date().getTime()));
        fileCabinetInformation.setOperateName(SecurityUtils.getUsername());
        fileCabinetInformation.setOperateId(SecurityUtils.getUserId());
        fileCabinetInformation.setBelongTo(SecurityUtils.getOrgId());
        fileCabinetInformation.setDepartmentName(SecurityUtils.getLoginUser().getUser().getDept().getDeptName());
        String subCabinetLocation = data.getIdPath().substring(37,73);//最顶层文件名称id
        String[] temp = data.getIdPath().split(",");
        if(temp.length>=4){
            throw new ServiceException("超出层级限制，文件柜最多修改到第3级！");
        }
        FileCabinetInformation newFileCabinet =new FileCabinetInformation();
        newFileCabinet.setCabinetId(temp[1]);
       /* FileCabinetInformation newFileCabinet =new FileCabinetInformation();
        boolean contains = data.getIdPath().indexOf(",") >= 0;
        if(contains==true){
            temp = data.getIdPath().split(",");
            newFileCabinet.setCabinetId(temp[1]);
            if(temp.length>=4){
                throw new ServiceException("超出层级限制，文件柜最多添加到第3级！");
            }
        }*/
        List<FileCabinetInformation> dataCabinetName = selectFileCabinetInformationList(newFileCabinet);
        fileCabinetInformation.setCabinetLocation(dataCabinetName.get(0).getCabinetLocation());
        fileCabinetInformation.setIdPath(data.getIdPath()+","+fileCabinetInformation.getCabinetId());
        return fileCabinetInformationMapper.insertFileCabinetInformation(fileCabinetInformation);
    }

    /**
     * 修改文件柜维护
     * 
     * @param fileCabinetInformation 文件柜维护
     * @return 结果
     */
    @Override
    public int updateFileCabinetInformation(FileCabinetInformation fileCabinetInformation)
    {
        FileCabinetInformation data = selectFileCabinetInformationByCabinetId(fileCabinetInformation.getSuperiorCabinetId());
        fileCabinetInformation.setSuperiorCabinetName(data.getCabinetName());//上级文件柜
        String subCabinetLocation = data.getIdPath().substring(37,73);//最顶层文件名称
        String[] temp = data.getIdPath().split(",");
        if(temp.length>=4){
            throw new ServiceException("超出层级限制，文件柜最多修改到第3级！");
        }
        FileCabinetInformation newFileCabinet =new FileCabinetInformation();
        newFileCabinet.setCabinetId(temp[1]);
        List<FileCabinetInformation> dataCabinetName = selectFileCabinetInformationList(newFileCabinet);
        fileCabinetInformation.setCabinetLocation(dataCabinetName.get(0).getCabinetLocation());
        /*fileCabinetInformation.setCabinetLocation(dataCabinetName.get(0).getCabinetLocation());
        if(subCabinetLocation.equals("650c44e1-b575-4b62-9d54-1ba9d1e8abb7")){
            fileCabinetInformation.setCabinetLocation("内部");
        }else if(subCabinetLocation.equals("02868977-1951-4772-a290-f6b5fe9ca76b")){
            fileCabinetInformation.setCabinetLocation("外部");
        }*/
        /**/
        fileCabinetInformation.setIdPath(data.getIdPath()+","+fileCabinetInformation.getCabinetId());
        return fileCabinetInformationMapper.updateFileCabinetInformation(fileCabinetInformation);
    }

    /**
     * 批量删除文件柜维护
     * 
     * @param cabinetIds 需要删除的文件柜维护主键
     * @return 结果
     */
    @Override
    public int deleteFileCabinetInformationByCabinetIds(String[] cabinetIds)
    {

        return fileCabinetInformationMapper.deleteFileCabinetInformationByCabinetIds(cabinetIds);
    }

    /**
     * 删除文件柜维护信息
     * 
     * @param cabinetId 文件柜维护主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteFileCabinetInformationByCabinetId(String cabinetId)
    {
        Long count = fileCabinetInformationMapper.countCabinetId(cabinetId);
        FileCabinetInformation data = new FileCabinetInformation();
        data.setSuperiorCabinetId(cabinetId);
        List<FileCabinetInformation> fileList = selectFileCabinetInformationList(data);
        int countDel = 0;
        if(count>0){
            throw new ServiceException("该文件柜已添加子文件，不允许删除！");
        }else if(fileList.size()>0){
            throw new ServiceException("该文件柜已添加子文件柜，不允许删除！");

        }
        return fileCabinetInformationMapper.deleteFileCabinetInformationByCabinetId(cabinetId);
    }

    /**
     * 获取当前企业的外部文件柜id
     * @param orgId
     * @return
     */
    @Override
    public String selectExternalFolder(Long orgId) {
      return fileCabinetInformationMapper.selectExternalFolderId(orgId);
    }

    @Override
    public String selectFileCabinetInformationByCabinetNameByExternal(String message, Long userId) {
        return fileCabinetInformationMapper.selectFileCabinetInformationByCabinetNameByExternal(message,userId);
    }
}
